#include "obj.h"

void obj::draw(){
	//sem este kod na otestovanie ci fakt treba vykreslovat 
	draw_obj(this);
}

void obj::calc_r(){
	r=0;
	FOR(i,verts.size())
		if (verts[i].x*verts[i].x+verts[i].y*verts[i].y>r*r)
			r = sqrt(verts[i].x*verts[i].x+verts[i].y*verts[i].y);
}

void obj::calc_col_verts(){
	col_verts.resize(verts.size());
	FOR(i,verts.size()){
		col_verts[i].x=x+verts[i].x*cos(rot)-verts[i].y*sin(rot);
		col_verts[i].y=y+verts[i].y*cos(rot)+verts[i].x*sin(rot);
	}
}


GLuint obj::load_jpg_texture (const char * filename) {
	
	GLuint new_texture;
	SDL_Surface *surface;	// This surface will tell us the details of the image
	GLenum texture_format;
	GLint  nOfColors;

	if ( (surface = IMG_LoadJPG_RW(SDL_RWFromFile(filename,"r"))) ) { 
//	if ( (surface = SDL_LoadBMP(_filename)) ) { 
		// Check that the image's width is a power of 2
		if ( (surface->w & (surface->w - 1)) != 0 ){
			debug("warning: image.jpg's width is not a power of 2");
			return -1;
		}		
		// Also check if the height is a power of 2
		if ( (surface->h & (surface->h - 1)) != 0 ){
			debug("warning: image.jpg's height is not a power of 2");
			return -1;
		}		
		// get the number of channels in the SDL surface
		nOfColors = surface->format->BytesPerPixel;
		if (nOfColors == 4){	 // contains an alpha channel
			if (surface->format->Rmask == 0x000000ff)
				texture_format = GL_RGBA;
			else
				texture_format = GL_BGRA;
		} 
		else 
			if (nOfColors == 3){	// no alpha channel
				if (surface->format->Rmask == 0x000000ff)
						texture_format = GL_RGB;
				else
						texture_format = GL_BGR;
			} 
			else{
				debug("warning: the image is not truecolor..  this will probably break");
				return -1;
			}
			
		// Have OpenGL generate a texture object handle for us
		glGenTextures( 1, &new_texture );

		// Bind the texture object
		glBindTexture( GL_TEXTURE_2D, new_texture );
	 
		// Set the texture's stretching properties
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	 
		// Edit the texture object's image data using the information SDL_Surface gives us
		glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );	 
	} 
	else {
		debug("SDL could not load %s: %s\n",filename, SDL_GetError());
		return -1;
	}
	// Free the SDL_Surface only if it was successfully created
	if ( surface )
		SDL_FreeSurface( surface );
	
	return new_texture;
}

GLuint obj::load_tga_texture (const char * filename) {
	
	GLuint new_texture;
	SDL_Surface *surface;	// This surface will tell us the details of the image
	GLenum texture_format;
	GLint  nOfColors;

	if ( (surface = IMG_LoadTGA_RW(SDL_RWFromFile(filename,"r"))) ) { 
		// Check that the image's width is a power of 2
		if ( (surface->w & (surface->w - 1)) != 0 ){
			debug("warning: image.tga's width is not a power of 2");
			return -1;
		}		
		// Also check if the height is a power of 2
		if ( (surface->h & (surface->h - 1)) != 0 ){
			debug("warning: image.tga's height is not a power of 2");
			return -1;
		}		
		// get the number of channels in the SDL surface
		nOfColors = surface->format->BytesPerPixel;
		if (nOfColors == 4){	 // contains an alpha channel
			if (surface->format->Rmask == 0x000000ff)
				texture_format = GL_RGBA;
			else
				texture_format = GL_BGRA;
		} 
		else 
			if (nOfColors == 3){	// no alpha channel
				debug("Warning: Loaded TGA has no apha channel");
				debug(filename);
					
				if (surface->format->Rmask == 0x000000ff)
						texture_format = GL_RGB;
				else
						texture_format = GL_BGR;
			} 
			else{
				debug("warning: the image is not truecolor..  this will probably break");
				return -1;
			}
			
		// Have OpenGL generate a texture object handle for us
		glGenTextures( 1, &new_texture );

		// Bind the texture object
		glBindTexture( GL_TEXTURE_2D, new_texture );
	 
		// Set the texture's stretching properties
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	 
		// Edit the texture object's image data using the information SDL_Surface gives us
		glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );	 
	} 
	else {
		debug("SDL could not load %s: %s\n",filename, SDL_GetError());
		return -1;
	}
	// Free the SDL_Surface only if it was successfully created
	if ( surface )
		SDL_FreeSurface( surface );
	
	return new_texture;
}


